package com.yic.module.mes.controller.admin.itemproduceroute;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.yic.framework.common.enums.CommonStatusEnum;
import com.yic.framework.common.util.collection.CollectionUtils;
import com.yic.module.mes.dal.dataobject.processRoute.ProcessRouteDO;
import com.yic.module.mes.service.processRoute.ProcessRouteService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.yic.framework.common.pojo.PageResult;
import com.yic.framework.common.pojo.CommonResult;
import static com.yic.framework.common.pojo.CommonResult.success;

import com.yic.framework.excel.core.util.ExcelUtils;

import com.yic.framework.operatelog.core.annotations.OperateLog;
import static com.yic.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.yic.module.mes.controller.admin.itemproduceroute.vo.*;
import com.yic.module.mes.dal.dataobject.itemproduceroute.ItemProduceRouteDO;
import com.yic.module.mes.convert.itemproduceroute.ItemProduceRouteConvert;
import com.yic.module.mes.service.itemproduceroute.ItemProduceRouteService;

@Tag(name = "管理后台 - 产品工艺路线")
@RestController
@RequestMapping("/mes/item-produce-route")
@Validated
public class ItemProduceRouteController {

    @Resource
    private ItemProduceRouteService itemProduceRouteService;
    @Resource
    private ProcessRouteService processRouteService;

    @PostMapping("/create")
    @Operation(summary = "创建产品工艺路线")
    @PreAuthorize("@ss.hasPermission('mes:item-produce-route:create')")
    public CommonResult<Long> createItemProduceRoute(@Valid @RequestBody ItemProduceRouteCreateReqVO createReqVO) {
        return success(itemProduceRouteService.createItemProduceRoute(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新产品工艺路线")
    @PreAuthorize("@ss.hasPermission('mes:item-produce-route:update')")
    public CommonResult<Boolean> updateItemProduceRoute(@Valid @RequestBody ItemProduceRouteUpdateReqVO updateReqVO) {
        itemProduceRouteService.updateItemProduceRoute(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除产品工艺路线")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:item-produce-route:delete')")
    public CommonResult<Boolean> deleteItemProduceRoute(@RequestParam("id") Long id) {
        itemProduceRouteService.deleteItemProduceRoute(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得产品工艺路线")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:item-produce-route:query')")
    public CommonResult<ItemProduceRouteRespVO> getItemProduceRoute(@RequestParam("id") Long id) {
        ItemProduceRouteDO itemProduceRoute = itemProduceRouteService.getItemProduceRoute(id);
        return success(ItemProduceRouteConvert.INSTANCE.convert(itemProduceRoute));
    }

    @GetMapping("/list")
    @Operation(summary = "获得产品工艺路线列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('mes:item-produce-route:query')")
    public CommonResult<List<ItemProduceRouteRespVO>> getItemProduceRouteList(@RequestParam("ids") Collection<Long> ids) {
        List<ItemProduceRouteDO> list = itemProduceRouteService.getItemProduceRouteList(ids);
        return success(ItemProduceRouteConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得产品工艺路线分页")
    @PreAuthorize("@ss.hasPermission('mes:item-produce-route:query')")
    public CommonResult<PageResult<ItemProduceRouteRespVO>> getItemProduceRoutePage(@Valid ItemProduceRoutePageReqVO pageVO) {
        PageResult<ItemProduceRouteDO> pageResult = itemProduceRouteService.getItemProduceRoutePage(pageVO);
        return success(ItemProduceRouteConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出产品工艺路线 Excel")
    @PreAuthorize("@ss.hasPermission('mes:item-produce-route:export')")
    @OperateLog(type = EXPORT)
    public void exportItemProduceRouteExcel(@Valid ItemProduceRouteExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ItemProduceRouteDO> list = itemProduceRouteService.getItemProduceRouteList(exportReqVO);
        // 导出 Excel
        List<ItemProduceRouteExcelVO> datas = ItemProduceRouteConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "产品工艺路线.xls", "数据", ItemProduceRouteExcelVO.class, datas);
    }

    @GetMapping("/list-all-simple")
    @Operation(summary = "获取产品工艺路线精简信息列表", description = "只包含被开启的，主要用于前端的下拉选项")
    public CommonResult<List<ItemProduceRouteSimpleRespVO>> getSimpleRouteList(@RequestParam("ids") Collection<Long> ids) {
        // 获得工艺路线列表，只要开启状态的
        List<ItemProduceRouteDO> list = itemProduceRouteService.getItemProduceRouteListByItemProduceId(ids);
        //提取工艺路线ID
        Set<Long> routeIds = CollectionUtils.convertSet(list, ItemProduceRouteDO::getRouteId);
        List<ProcessRouteDO> processRouteList = processRouteService.getProcessRouteList(routeIds);
        //组装数据
        List<ItemProduceRouteSimpleRespVO> resp = ItemProduceRouteConvert.INSTANCE.convertList04(list);
        Map<Long, ProcessRouteDO> routeDOMap = CollectionUtils.convertMap(processRouteList, ProcessRouteDO::getId);
        if (CollectionUtil.isNotEmpty(resp)) {
            resp.forEach(item -> {
                ProcessRouteDO processRouteDO = routeDOMap.get(item.getRouteId());
                if (ObjectUtil.isNotEmpty(processRouteDO)) {
                    item.setRouteCode(processRouteDO.getCode());
                    item.setRouteName(processRouteDO.getName());
                }
            });
        }
        return success(resp);
    }
}
